package com.cm.projectx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cm.projectx.common.entity.UserContext;
import com.cm.projectx.entity.MAINJcdgl2XFyOitHD;
import com.cm.projectx.entity.dto.MonitoringPointsDto;
import com.cm.projectx.entity.dto.MonitoringPointsTypeDto;
import com.cm.projectx.mapper.MAINJcdgl2XFyOitHDMapper;
import com.cm.projectx.service.IMAINJcdgl2XFyOitHDService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 二期监测点列表实现
 */
@Service
public class MAINJcdgl2XFyOitHDServiceImpl implements IMAINJcdgl2XFyOitHDService {

    @Autowired
    private MAINJcdgl2XFyOitHDMapper mapper;


    @Override
    public List<MonitoringPointsTypeDto> getMonitoringPointsList() {
        //数据权限
//        String dataScope = UserContext.getUserDataScope();
        LambdaQueryWrapper<MAINJcdgl2XFyOitHD> typeWrapper = Wrappers.lambdaQuery();
        typeWrapper.isNull(MAINJcdgl2XFyOitHD::getSelectRelated1757474711065);
//        if (StringUtils.isNotBlank(dataScope)) {
//            typeWrapper.like(MAINJcdgl2XFyOitHD::getInput1756457125178,dataScope);
//        }
        List<MAINJcdgl2XFyOitHD> area = mapper.selectList(typeWrapper);

        // 转换为ID到类别名称的映射
        Map<String, String> typeIdToNameMap = area.stream()
                .collect(Collectors.toMap(
                        MAINJcdgl2XFyOitHD::getId,
                        MAINJcdgl2XFyOitHD::getInput1756457125178
                ));

        LambdaQueryWrapper<MAINJcdgl2XFyOitHD> productWrapper = Wrappers.lambdaQuery();
        productWrapper.isNotNull(MAINJcdgl2XFyOitHD::getSelectRelated1757474711065);
        List<MAINJcdgl2XFyOitHD> jcds = mapper.selectList(productWrapper);

        // 3. 按商品类别分组
        Map<String, MonitoringPointsTypeDto> typeMap = new LinkedHashMap<>();

        for (MAINJcdgl2XFyOitHD jcd : jcds) {
            String typeId = jcd.getSelectRelated1757474711065();
            String typeName = typeIdToNameMap.get(typeId);

            if (typeName == null) {
                continue; // 跳过没有对应类别的商品
            }
            // 如果该类别尚未在map中，则创建新条目
            if (!typeMap.containsKey(typeName)) {
                MonitoringPointsTypeDto typeDto = new MonitoringPointsTypeDto();
                typeDto.setArea(typeName);
                typeDto.setMonitoringPointsList(new ArrayList<>());
                typeMap.put(typeName, typeDto);
            }

            MonitoringPointsTypeDto typeDto = typeMap.get(typeName);
            // 创建商品DTO并添加到列表
            MonitoringPointsDto monitoringPointsDto = new MonitoringPointsDto();
            monitoringPointsDto.setId(jcd.getId());
            monitoringPointsDto.setMonitoringPointsName(jcd.getInput1756457125178());

            typeDto.getMonitoringPointsList().add(monitoringPointsDto);
        }
        return new ArrayList<>(typeMap.values());
    }
}
